package com.example.controller;

import com.example.common.Result;
import com.example.entity.Address;
import com.example.entity.Checkpoint;
import com.example.entity.Chengji;
import com.example.service.CheckpointService;
import com.example.service.ChengjiService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 关卡前端接口
 */
@RestController
@Api(value = "用户成绩", tags = {"成绩信息的增删改查"})
@RequestMapping("/chengji")
public class ChengjiController {

    @Resource
    private ChengjiService chengjiService;

    @ApiOperation(value = "随机获取一名学生成绩", notes = "返回学生成绩")
    @GetMapping("/random")
    public Chengji getRandomChengji(){
        Chengji chengji = chengjiService.getRandomChengji();
        if (chengji == null){
            throw new ResponseStatusException(HttpStatus.NO_CONTENT);
        }
        return chengji;
    }

    /**
     * 新增
     */
    @ApiOperation(value = "增加成绩信息", notes = "返回是否成功")
    @PostMapping("/add")
    public Result add(@RequestBody Chengji chengji) {
        chengjiService.add(chengji);
        return Result.success();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除成绩信息", notes = "返回是否成功")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要删除的成绩id,如'1'"
            )
    })
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        chengjiService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除成绩信息", notes = "返回是否成功")
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        chengjiService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新成绩信息", notes = "返回是否成功")
    @PutMapping("/update")
    public Result updateById(@RequestBody Chengji chengji) {
        chengjiService.updateById(chengji);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "通过id查询成绩信息", notes = "返回Models中的成绩信息类型")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要查询的成绩信息id,如'1'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Chengji.class,reference = "Chengji")
    })
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Chengji chengji = chengjiService.selectById(id);
        return Result.success(chengji);
    }

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询所有符合特征的成绩信息", notes = "返回Models中的成绩信息类型的数组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Chengji.class,reference = "Chengji")
    })
    @GetMapping("/selectAll")
    public Result selectAll(Chengji chengji ) {
        List<Chengji> chengjiList = chengjiService.selectAll(chengji);
        return Result.success(chengjiList);
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询所有符合特征的成绩信息", notes = "返回Models中的成绩信息的分页数组")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "pageNum",
                    value = "要查询的页码,默认为'1'"
            ),
            @ApiImplicitParam(
                    name = "pageSize",
                    value = "每页尺寸,默认为'10'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Chengji.class,reference = "Chengji")
    })
    @GetMapping("/selectPage")
    public Result selectPage(Chengji chengji,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Chengji> pageInfo = chengjiService.selectPage(chengji, pageNum, pageSize);
        return Result.success(pageInfo);
    }

}
